<?php
/**
 * 评论控制层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: CommentAction.class.php 7647 2011-06-04 08:56:45Z yemusheng $
 * 
 */
class CommentAction extends ThinkAPK_Action_Mall {
	
	/**
	 * 首页评论分页
	 *
	 */
	function commentIndex(){
		$page = intval($_POST['page']);
		$this->assign('comment', D('Comment')->indexComment($page));
		$this->success($this->fetch());
	}
	
	/* ajax提交咨询
	 *
	*/
	public function submitComment(){
		$now_time=time();
		$data=array();
		$data['id_value'] = isset ( $_GET ['id'] ) ? intval ( $_GET['id'] ) : 0;
		//同一商品限制提交时间，防止恶意提交/重复提交/机器人提交 等	，限制同一商品60秒内不能提交
		if(isset($_SESSION['send_time_'.$data['id_value']]) && $now_time-$_SESSION['send_time_'.$data['id_value']]<=60){
			$this->error('您的咨询已提交，请勿重复提交！<BR>如果您还有其它疑问，请在一分钟后再提交');
		}
		
		$data['user_name'] = (empty($_GET['username']) || $_GET['username'] == 'undefined') ? ($_SESSION['user_name']?$_SESSION['user_name']:'匿名用户'): trim($_GET['username']);
		$data['mobile'] = isset ( $_GET ['mobile'] ) ? $_GET ['mobile'] : 0;
		$data['email'] = isset ( $_GET ['email'] ) ? $_GET ['email'] : '';
		$data['qq'] = isset ( $_GET ['qq'] ) ? $_GET ['qq'] : '';
		$data['sex'] = isset ( $_GET ['sex'] ) ? $_GET ['sex'] : 0;
		$data['content'] = isset ( $_GET ['content'] ) ? strip_tags($_GET ['content']) : '';
		$data['user_id']  = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0; 
		
		//默认值
		$data['comment_type']=0;
		$data['comment_rank']=5;
		$data['add_time']=$now_time;
		$data['ip_address']=get_client_ip();
		$data['status']=0;
		$data['parent_id']=0;
		$CM=new CommentModel();
		$result=$CM->add($data);
		if($result){
			//同一商品限制提交时间，防止恶意提交/重复提交/机器人提交 等
			$_SESSION['send_time_'.$data['id_value']]=$now_time;
			$this->success('提交成功！感谢您的支持<BR>我们会尽快给你答复！');
		}else{
			$this->error('提交失败！请检查您输入的内容是否含有特殊符号！'.$CM->getLastSQL());
		}
	}
	
	/*
	 * ajax获取咨询列表
	*/
	public function getComments(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		$type=is_numeric($_GET['type'])?$_GET['type']:0;
		
		//取咨询列表
		$comments=D('Comment')->getComments($goods_id,$type);
		if($comments) $this->assign('comments',$comments);
		$total=$comments['total'];
		unset($comments);
		$html=$this->fetch('/common:goods_comment_list');
		
		$this->success($html,array('total'=>$total));
	}
	/*
	 *ajax取提交咨询表单
	*/
	public function getCommentForm(){
		
		if($_SESSION['user_name']) $this->assign('user_name',$_SESSION['user_name']);
		$this->assign('comment_type','0');
		$form=$this->fetch('/common:goods_comment_form');
		$this->success($form);
	}
}
?>